package com.jzo2o.orders.dispatch.demo;

import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.orders.dispatch.model.dto.ServeProviderDTO;

import java.util.List;

/**
 * 抽象策略类
 *
 * @author Mr.Z
 * @version 1.0
 * @date 2023/11/24 11:53
 */
public abstract class AbstractStrategyImpl implements IProcessStrategy {

	// 派单规则责任链的头部
	private final IProcessRule processRule;

	public AbstractStrategyImpl() {
		this.processRule = getRules();
	}

	/**
	 * 设置派单规则
	 *
	 * @return:
	 */
	protected abstract IProcessRule getRules();

	@Override
	public ServeProviderDTO getPrecedenceServeProvider(List<ServeProviderDTO> serveProviderDTOS) {
		// 1.判空
		if (CollUtils.isEmpty(serveProviderDTOS)) {
			return null;
		}

		// 2.根据优先级获取高优先级别的
		serveProviderDTOS = processRule.filter(serveProviderDTOS);

		// 3.数据返回
		// 3.1.唯一高优先级直接返回
		int size;
		if ((size = CollUtils.size(serveProviderDTOS)) == 1) {
			return serveProviderDTOS.get(0);
		}
		// 3.2.多个高优先级随即将返回
		int randomIndex = (int) (Math.random() * size);
		return serveProviderDTOS.get(randomIndex);
	}
}